﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Test test = new Test();
            InitializeComponent();
            
            Binding bind = new Binding();
            bind.Source = test;
            bind.Path = new PropertyPath("Flow");
            BindingOperations.SetBinding(test, Test.FlowDependencyProperty, bind);
            
            test.Flow = 3;
            string s = "FlowDependency:" + test.FlowDependency.ToString();
            MessageBox.Show(s);
            test.Flow = 30;
            s = "FlowDependency:" + test.FlowDependency.ToString();
            MessageBox.Show(s);
        }
    }
    public class Test : DependencyObject, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        private float flow;
        public float Flow
        {
            get { return flow; }
            set
            {
                flow = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Flow"));
            }
        }

        public static readonly DependencyProperty FlowDependencyProperty = DependencyProperty.Register("FlowDependency", typeof(float), typeof(Test), new FrameworkPropertyMetadata());
        public float FlowDependency
        {
            get { return (float)GetValue(FlowDependencyProperty); }
            set { SetValue(FlowDependencyProperty, value); }
        }
    }
}
